package elitech.vietnam.weatherfashion.dialog;

import java.io.IOException;

import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.service.PHPSoapServiceCaller;

public class DialogUserInfo extends Dialog implements View.OnClickListener {

	private TextView text_username, text_status;
	private Button button_close;
	private ImageView image_avatar;
	private int memID;
	private MemberDTO member;

	public DialogUserInfo(Context context, int memID) {
		super(context, R.style.Dialog);
		this.memID = memID;
	}

	public DialogUserInfo(Context context, MemberDTO member) {
		super(context, R.style.Dialog);
		this.member = member;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_userinfo);

		text_username = (TextView) findViewById(R.id.dialog_userinfo_text_name);
		text_status = (TextView) findViewById(R.id.dialog_userinfo_text_status);
		image_avatar = (ImageView) findViewById(R.id.dialog_userinfo_image_avatar);
		button_close = (Button) findViewById(R.id.dialog_userinfo_button_close);

		button_close.setOnClickListener(this);
		if (member == null)
			new GetMemberInfo().execute();
		else
			loadMember();
	}

	private void loadMember() {
		text_username.setText(member.getMemberName());
		text_status.setText(member.getMemberProfile());
		UrlImageViewHelper.setUrlDrawable(image_avatar,
				Const.SERVER_IMAGE_URL + member.getMemberPic(),
				R.drawable.avatar);
	}

	@Override
	public void onClick(View arg0) {
		if (arg0.getId() == R.id.dialog_userinfo_button_close)
			dismiss();
	}

	class GetMemberInfo extends AsyncTask<Integer, Integer, Boolean> {
		@Override
		protected Boolean doInBackground(Integer... params) {
			PHPSoapServiceCaller service = new PHPSoapServiceCaller();
			try {
				member = service.getInfoMember(memID);
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
			return null;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if (result != null) {
				new WarningDialog.Builder(getContext())
				.setOnClick(new WarningDialog.OnButtonClick() {
					@Override
					public void doClick() {
						dismiss();
					}
				}).createTimeOutDialog().show();
				return;
			}
			if (member == null)
				return;
			loadMember();
		}
	}
}
